package top.chengdongqing.mishop.order.review.vo

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import top.chengdongqing.mishop.entities.OrderItem
import top.chengdongqing.mishop.entities.ProductReview
import java.io.Serializable

data class ProductReviewVO(
    val id: Int,
    val name: String,
    val pictureUrl: String,
    val rating: Int?,
    val content: String?,
    val photoUrls: List<String>?,
    val isAnonymous: Boolean?
) : Serializable {
    companion object {
        fun of(orderItem: OrderItem, productReview: ProductReview?): ProductReviewVO {
            return ProductReviewVO(
                id = orderItem.productId,
                name = orderItem.productName,
                pictureUrl = orderItem.pictureUrl,
                rating = productReview?.rating,
                content = productReview?.content,
                photoUrls = productReview?.photoUrls?.let {
                    jacksonObjectMapper().readValue(it)
                },
                isAnonymous = productReview?.isAnonymous
            )
        }
    }
}